package com.yf.modules.system.controller;

import com.yf.Utils.PageUtil;
import com.yf.domain.security.Role;
import com.yf.model.query.RoleQueryCriteria;
import com.yf.modules.system.service.api.RoleService;
import com.yf.response.ResponseBody;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * @author chentianwei
 * @date 2024-10-24 13:14
 * @description 角色RestFul
 */
@RestController
@RequestMapping("api/roles")
@Slf4j
@Tag(name = "RoleApi", description = "角色Api")
public class RoleController {

    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @GetMapping("")
    @Operation(summary = "获取角色", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<Object> getRole(RoleQueryCriteria criteria) {
        List<Role> roleList = roleService.queryRole(criteria);
        return ResponseEntity.ok().body(ResponseBody.success("操作成功", PageUtil.toPage(roleList, roleList.size())));
    }

    @PostMapping("/add")
    @Operation(summary = "添加角色", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> addRole(@RequestBody Role role) {
        roleService.addRole(role);
        return ResponseEntity.ok().body(ResponseBody.success("添加成功"));
    }

    @PutMapping("/edit")
    @Operation(summary = "修改角色", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> editRole(@RequestBody Role role) {
        roleService.editRole(role);
        return ResponseEntity.ok().body(ResponseBody.success("修改成功"));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除角色", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> deleteRole(@RequestBody Set<Long> ids) {
        roleService.deleteRole(ids);
        return ResponseEntity.ok().body(ResponseBody.success("删除成功"));
    }

    @GetMapping("/download")
    @Operation(summary = "下载角色", security = @SecurityRequirement(name = "authScheme"))
    public void downloadRole(HttpServletResponse response, RoleQueryCriteria criteria) throws IOException {
        roleService.download(response, criteria);
    }

    @PutMapping("/linkMenu")
    @Operation(summary = "角色关联菜单", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> linkRoleMenu(@RequestBody Role role){
        roleService.linkRoleMenu(role);
        return ResponseEntity.ok().body(ResponseBody.success("关联成功"));
    }


}
